package com.example;

import javafx.print.Collation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@SpringBootApplication
public class MailApplication implements CommandLineRunner {

    @Autowired
    private JavaMailSender mailSender;
    private AtomicInteger counter = new AtomicInteger(0);

    public static void main(String[] args) {
        SpringApplication.run(MailApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        // QQ邮箱收件人列表
        List<String> recipients = Files.readAllLines(Paths.get("doc/qq6.txt"))
                .stream()
                .map(String::trim)
                .filter(line -> !line.isEmpty())
                .map(line -> line.replace(",", "")) // 去除逗号
                .map(line -> line.endsWith("@qq.com") ? line : line + "@qq.com") // 补全邮箱后缀
                .distinct().sorted().collect(Collectors.toList());

        // 邮件主题
        String subject = "同城车友会";
        // 邮件内容（HTML格式）
        String htmlContent = "<h2>如果你想周末过得有意思，不防加入我们</h2>"
//                + "<p> <b>人生不过三万多天，嗨起来呀，定期组织各种车友聚会，交友活动</b></p>"
                + "<p> <b style='color:red;font-size: 20px;'>详情咨询:15021224494(微信同号)</b></p>"
                +"<img src='cid:MainImage3' style='width:1000px; height:700px;'/>"
                + "<img src='cid:MainImage' style='width:1000px; height:700px;'/>"
              + "<img src='cid:MainImage1' style='width:1000px; height:700px;'/>"
              +"<img src='cid:MainImage2' style='width:1000px; height:700px;'/>"

              +"<img src='cid:MainImage4' style='width:1000px; height:700px;'/>"
              +"<img src='cid:MainImage5' style='width:1000px; height:700px;'/>"
              +"<img src='cid:MainImage6' style='width:1000px; height:700px;'/>"
              +"<img src='cid:MainImage7' style='width:1000px; height:700px;'/>"
                ;
//                + "<div><img src='cid:wechatImage' style='width:100px; height:150px;float: right'/></div>";
//                + "<p style='color:red;font-size: 20px; text-align: center;'>服务|品质|传承|创新</p>";

        System.out.println("共有数据: " + recipients.size());
        for (String to : recipients) {
            if(!"1289108384@qq.com".equals(to)){
//                continue;
            }
            boolean success = sendHtmlMail(to, subject, htmlContent);
            if(!success){
                break;
            }
        }
        System.out.println("发送成功: " + counter.get());
//        recipients.forEach(to ->executorService.execute(() -> {
//            if(!"1289108384@qq.com".equals(to)){
////                continue;
//            }
//            sendHtmlMail(to, subject, htmlContent);
//        }));
    }

    private boolean sendHtmlMail(String to, String subject, String htmlContent) {
        boolean success = false;
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom("chat_xiangxian@163.com","同城车友聚会"); // 替换为你的163邮箱
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlContent, true);
            // 绑定图片，logoImage是cid标识符
            helper.addInline("MainImage", new ClassPathResource("static/social/1.jpg"));
            helper.addInline("MainImage1", new ClassPathResource("static/social/2.jpg"));
            helper.addInline("MainImage2", new ClassPathResource("static/social/3.jpg"));
            helper.addInline("MainImage3", new ClassPathResource("static/social/4.jpg"));

            helper.addInline("MainImage4", new ClassPathResource("static/social/5.jpg"));
            helper.addInline("MainImage5", new ClassPathResource("static/social/6.jpg"));
            helper.addInline("MainImage6", new ClassPathResource("static/social/9.jpg"));
            helper.addInline("MainImage7", new ClassPathResource("static/social/8.jpg"));
            mailSender.send(message);
            success = true;
            System.out.println("邮件已发送至: " + to);
            counter.addAndGet(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }
} 